package org.gwtEss.other;

import com.google.gwt.event.dom.client.HasMouseDownHandlers;
import com.google.gwt.event.dom.client.HasMouseMoveHandlers;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Widget;

public class DraggableWidget extends Draggable {
	private Widget widget;
	private int startX, startY;

	public DraggableWidget(Widget widget) {
		setWidget(widget);
	}
	@Override
	protected boolean isPreventDefault(int x, int y) {
		return true;
	}
	@Override
	public void onDragStart(int mouseX, int clientY) {
		startX = widget.getAbsoluteLeft();
		startY = widget.getAbsoluteTop();
	}
	
	@Override
	public void onDragEnd(int mouseX, int mouseY, int deltaX, int deltaY) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onDrag(int mouseX, int mouseY, int deltaX, int deltaY) {
		DOM.setStyleAttribute(widget.getElement(), "left", (startX - deltaX) + "px");
		DOM.setStyleAttribute(widget.getElement(), "top", (startY - deltaY) + "px");
//		widget.getElement().setAttribute("left", (startX + deltaX) + "px");
//		widget.getElement().setAttribute("top", (startY + deltaY) + "px");
	}
	@Override
	public void setWidget(HasMouseDownHandlers widget) {
//		assert false: "use public void setWidget(HasWidgetResizerRequiredHandlers resizerWidget) instead";
		setWidget((Widget)widget);
	}	
	public void setWidget(Widget widget) {
		assert widget instanceof Widget && widget instanceof HasMouseDownHandlers && widget instanceof HasMouseMoveHandlers: "ResizableWidget should be instance of HasWidgetResizerRequiredHandlers and Widget";
		this.widget = (Widget)widget;
//		addMouseMoveHandler((HasMouseMoveHandlers)widget);
		super.setWidget((HasMouseDownHandlers)widget);
	}
	
}
